from django.db import models
from valar.models.core import VModel

class EmissionFactor(VModel):
    name = models.CharField(max_length=100, null=True, verbose_name='因子名称')
    value = models.FloatField(null=True, verbose_name='排放因子值')
    unit = models.CharField(max_length=50, null=True, verbose_name='单位')
    category = models.CharField(max_length=50, null=True, verbose_name='类别')
    class Meta:
        verbose_name = '排放因子'

class RawMaterial(VModel):
    name = models.CharField(max_length=100, null=True, verbose_name='原料名称')
    mass = models.FloatField(null=True, verbose_name='质量(吨)')
    factor = models.FloatField(null=True, verbose_name='排放因子(kgCO2/吨)')
    emission = models.FloatField(default=0, null=True, verbose_name='排放量(kgCO2)')
    class Meta:
        verbose_name = '原料'

class EnergyConsumption(VModel):
    ENERGY_TYPES = [
        ('electricity', '电力'),
        ('steam', '蒸汽'),
        ('natural_gas', '天然气'),
    ]
    
    name = models.CharField(max_length=100, null=True, verbose_name='能源名称')
    energy_type = models.CharField(max_length=20, choices=ENERGY_TYPES, null=True, verbose_name='能源类型')
    amount = models.FloatField(null=True, verbose_name='消耗量')
    factor = models.FloatField(null=True, verbose_name='排放因子')
    emission = models.FloatField(default=0, null=True, verbose_name='排放量(kgCO2)')
    
    @property
    def unit(self):
        """根据能源类型返回固定单位"""
        unit_map = {
            'electricity': 'kWh',
            'steam': 't',
            'natural_gas': 'm³'
        }
        return unit_map.get(self.energy_type, '')
    
    @property
    def factor_unit(self):
        """根据能源类型返回排放因子单位"""
        factor_unit_map = {
            'electricity': 'kgCO2/kWh',
            'steam': 'kgCO2/t',
            'natural_gas': 'kgCO2/m³'
        }
        return factor_unit_map.get(self.energy_type, '')
    
    class Meta:
        verbose_name = '能耗'

class AuxMaterial(VModel):
    name = models.CharField(max_length=100, null=True, verbose_name='辅材名称')
    mass = models.FloatField(null=True, verbose_name='质量(吨)')
    factor = models.FloatField(null=True, verbose_name='排放因子(kgCO2/吨)')
    emission = models.FloatField(default=0, null=True, verbose_name='排放量(kgCO2)')
    class Meta:
        verbose_name = '辅助材料'

class Transport(VModel):
    name = models.CharField(max_length=100, null=True, verbose_name='运输名称')
    mass = models.FloatField(null=True, verbose_name='货物质量(吨)')
    distance = models.FloatField(null=True, verbose_name='运输距离(公里)')
    factor = models.FloatField(null=True, verbose_name='排放因子(kgCO2/(t·km))')
    emission = models.FloatField(default=0, null=True, verbose_name='排放量(kgCO2)')
    class Meta:
        verbose_name = '运输'

class CalculationResult(VModel):
    batch_name = models.CharField(max_length=200, null=True, verbose_name='批次名称')
    product_mass = models.FloatField(null=True, verbose_name='产品产量(吨)')
    E_raw = models.FloatField(default=0, null=True, verbose_name='原料排放(kgCO2)')
    E_energy = models.FloatField(default=0, null=True, verbose_name='能耗排放(kgCO2)')
    E_aux = models.FloatField(default=0, null=True, verbose_name='辅材排放(kgCO2)')
    E_transport = models.FloatField(default=0, null=True, verbose_name='运输排放(kgCO2)')
    E_total = models.FloatField(default=0, null=True, verbose_name='总排放(kgCO2)')
    footprint = models.FloatField(null=True, verbose_name='碳足迹(kgCO2/吨)')
    class Meta:
        verbose_name = '计算结果'